package com.ssfeng.youxia.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import com.ssfeng.youxia.services.EnvironmentService;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;

@Configuration
@ComponentScan(basePackages = {
		"com.ssfeng.youxia.dao"})
@PropertySource({"classpath:mysql.properties"})
@EnableTransactionManagement(proxyTargetClass=true)
public class SpringDatabaseConfig {

	@Autowired
	private EnvironmentService environmentService;


	private Environment environment;

	@Bean
	public DataSource getDataSource() {

		/***
		 *
		 */
		/*DriverManagerDataSource dataSource = new DriverManagerDataSource();
		dataSource.setDriverClassName("com.mysql.jdbc.Driver");
		dataSource.setUrl(environmentService.getProperty("db.url"));
		dataSource.setUsername(environmentService.getProperty("db.user"));
		dataSource.setPassword(environmentService.getProperty("db.password"));*/


		/*DruidDataSource dataSource = new DruidDataSource();
		dataSource.setDriverClassName(environment.getProperty("db.driver"));
		dataSource.setUrl(environment.getProperty("db.url"));
		dataSource.setUsername(environment.getProperty("db.user"));
		dataSource.setPassword(environment.getProperty("db.password"));
		dataSource.setMaxActive(Integer.parseInt(environment.getProperty("db.pool.maxActive")));
		dataSource.setMinIdle(Integer.parseInt(environment.getProperty("db.pool.minIdle")));*/
		DruidDataSource dataSource = new DruidDataSource();
		dataSource.setDriverClassName("com.mysql.jdbc.Driver");
		dataSource.setUrl("jdbc:mysql://localhost:3306/youxia?useOldAliasMetadataBehavior=true&useUnicode=true&characterEncoding=utf8&tinyInt1isBit=false");
		dataSource.setUsername("json");
		dataSource.setPassword("123456");
		dataSource.setMaxActive(10);
		dataSource.setMinIdle(3);

		return dataSource;
	}

	/*@Bean
	public SqlSessionFactory sqlSessionFactory() throws Exception {
		ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
		SqlSessionFactoryBean sqlSessionFactory = new SqlSessionFactoryBean();
		sqlSessionFactory.setDataSource(getDataSource());
		sqlSessionFactory.setMapperLocations(resolver.getResources("classpath:mapper/*.xml"));
		sqlSessionFactory.setTypeAliasesPackage("com.ssfeng.youxia.mapper");
		return (SqlSessionFactory) sqlSessionFactory.getObject();
	}*/

	@Bean
	@Autowired
	public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
		MybatisConfiguration configuration = new MybatisConfiguration();
		MybatisSqlSessionFactoryBean sqlSessionFactory = new MybatisSqlSessionFactoryBean();
//		DataSource dataSource = getDataSource();
		sqlSessionFactory.setDataSource(dataSource);
		Resource[] resources = new PathMatchingResourcePatternResolver()
				.getResources("classpath:mapper/*.xml");
		sqlSessionFactory.setMapperLocations(resources);
//		sqlSessionFactory.setConfiguration(configuration);
		sqlSessionFactory.setPlugins(new Interceptor[]{paginationInterceptor()});
		return sqlSessionFactory.getObject();
	}


	@Bean
	@Autowired
	public DataSourceTransactionManager transactionManager(DataSource dataSource) {
		return new DataSourceTransactionManager(dataSource);
	}

	@Bean
	@Autowired
	public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory){
		SqlSessionTemplate sessionTemplate = new SqlSessionTemplate(sqlSessionFactory);
		return sessionTemplate;
	}

	@Bean
	public MapperScannerConfigurer mapperScannerConfigurer(){
		MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
		mapperScannerConfigurer.setBasePackage("com.ssfeng.youxia.mapper");
		return mapperScannerConfigurer;
	}

	public PaginationInterceptor paginationInterceptor() {
		PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
		paginationInterceptor.setLimit(-1);
		//paginationInterceptor.setLocalPage(true);// 开启 PageHelper 的支持
		return paginationInterceptor;
	}

	/*@Bean
	public void setEnvironment(Environment environment) {
		this.environment = environment;
	}*/

	@Autowired
	public void setEnvironmentService(EnvironmentService environmentService) {
		this.environmentService = environmentService;
	}

}
